<template>
  <div>
    <!-- <el-button type="danger">批量删除</el-button> -->
    <el-button type="primary" size="small" @click="toAdd">添加顾客</el-button>
    <!-- 搜索框 -->
    <el-input
      size="small"
      v-model="search_realname"
      placeholder="请输入要查找的姓名"
      style="width: 200px; display: inline-block"
    ></el-input>
      <el-input
      size="small"
      v-model="search_telephone"
      placeholder="请输入要查找的手机号"
      style="width: 200px; display: inline-block"
    ></el-input>
    <el-button type="primary" size="small" @click="toSearch"
      >模糊查询</el-button
    >
    <!-- {{customer}} -->
    <el-table :data="customer" style="width: 100%">
      <el-table-column prop="id" label="ID" width="180"> </el-table-column>
      <el-table-column prop="realname" label="姓名" width="180">
      </el-table-column>
      <el-table-column prop="gender" label="性别" width="180">
      </el-table-column>
      <el-table-column prop="telephone" label="电话"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click="detailsHandle(scope.row.id)" type="text" size="small">详情</el-button>
          <el-button @click="editHandle(scope.row)" type="text" size="small">编辑</el-button>
          <el-button @click="delHandle(scope.row.id)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页插件 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="this.list.pageSize"
      :total="total"
      :current-page.sync="page" 
      @current-change="changePage"
    ></el-pagination>
    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="姓名">
          <el-input v-model="form.realname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="form.gender" placeholder="请选择性别">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.telephone" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 模态框结束 -->
  </div>
</template>
<script>
import { mapActions, mapState } from "vuex";
export default {
  data() {
    return {
      // customers:[],
      title:"",// 模态框的title
      search_telephone:"",
      search_realname:"",
      form: {},
      dialogFormVisible: false,
      page:0,
      list: {
        page: 0,
        pageSize: 5,
      },
    };
  },
  created() {
    // 查询所有顾客
    // this.findAllCustomer();
    // 分页查找顾客信息
    this.queryCustomer(this.list);
  },
  computed: {
    ...mapState("customer", ["total", "customer", "customers"]),
  },
  methods: {
    // vuex store中的方法
    ...mapActions("customer", [
      "findAllCustomer",
      "queryCustomer",
      "saveCustomer",
      "delCustomerById",
    ]),
    // 分页查询
    changePage(page) {
      this.list.page = page - 1;
      // {page = 1,pageSize:5}
      this.queryCustomer(this.list);
    },
    // 添加顾客
    toAdd() {
      // 打开模态框
      this.dialogFormVisible = true;
      // 设置模态框标题
      this.title =  "添加顾客信息";
      // 清空表单
      this.form = {};
    },
    submit(from) {
      // 提交表单到后台
      this.saveCustomer(this.form);
      // 关闭模态框
      this.dialogFormVisible = false;
      // 处理添加顾客后，分页插件页码的依然停留在添加顾客之前所在的页面的bug
      this.page = 1;
    },
    // 删除单个顾客
    delHandle(id) {
      this.$confirm("此操作将永久删除该顾客, 是否确定?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delCustomerById(id);
          // 处理删除数据后，分页插件页码的依然停留在被删除的数据所在的页面的bug
          this.page = 1;
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    editHandle(row){
      // 打开模态框
      this.dialogFormVisible = true;
      // 设置模态框title
      this.title = "编辑顾客信息";
      // 把当前行信息设置到表单上
      this.form = row;
    },
    detailsHandle(id){
      // 跳转到详情页（导航-vueRouter）
      this.$router.push({name:"details",params:{id:id}})
    },
    toSearch() {
      this.list = {
        page: 0,
        pageSize: 5,
        realname:this.search_realname,
        telephone:this.search_telephone,
      };
      this.queryCustomer(this.list);
    },
  },
};
</script>
<style scoped>
</style>
